
package com.meis.widget.utils;


import ohos.agp.colors.RgbColor;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Point;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;

/**
 * @Author: AriesHoo on 2018/7/19 9:49
 * @E-Mail: AriesHoo@126.com
 * Function: 资源文件获取帮助类
 * Description:
 */
public class ResourceUtil {
    private Context mContext;

    public ResourceUtil(Context context) {
        this.mContext = context;
    }

    public String getText(int res) {
        String txt = null;
        try {
            txt = mContext.getString(res);
        } catch (Exception e) {

        }
        return txt;
    }

    public String[] getTextArray(int res) {
        String[] result = new String[0];
        try {
            result = mContext.getStringArray(res);
        } catch (Exception e) {
        }
        return result;
    }

    public ShapeElement getDrawable(int res) {
        return new ShapeElement(mContext, res);
    }

    /**
     * 获取Image
     * @param res 资源id
     * @return Element
     */
    public Element getImageDrawable(int res) {
        PixelMapElement drawable = null;
        try {
            drawable = new PixelMapElement(
                    mContext.getResourceManager().getResource(res));
        } catch (IOException ioException) {
            ioException.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        }
        return drawable;
    }

    /**
     * 通过颜色id 获取elemet对象
     *
     * @param res 颜色id
     * @return Element
     */
    public Element getColorDrawable(int res) {
        int color = getColor(res);
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(RgbColor.fromArgbInt(color));
        return shapeElement;
    }

    /**
     * 获取资源PixelMap
     *
     * @param res 资源ID
     * @return PixelMapElement
     */
    public PixelMapElement getPixelMapElement(int res) {
        try {
            return new PixelMapElement(mContext.getResourceManager().getResource(res));
        } catch (IOException ioException) {
            ioException.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取颜色
     * @param res 资源id
     * @return int color
     */
    public int getColor(int res)  {
        int result = 0;
        try {
            result = mContext.getResourceManager().getElement(res).getColor();
        } catch (IOException ioException) {
            ioException.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        } catch (WrongTypeException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取状态栏高度
     *
     * @param context context
     * @return int StatusBarHeight
     */
    public static int getStatusBarHeight(Context context) {
        int result = 0;
        Point point1 = new Point();
        Point point2 = new Point();
        Display display = DisplayManager.getInstance().getDefaultDisplay(context).get();
        display.getSize(point1);
        display.getRealSize(point2);
        result = (int) (point2.getPointY() - point1.getPointY());
        return result;
    }
}
